package cn.kgc.service;

import cn.kgc.pojo.DmItem;
import cn.kgc.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.kgc.mapper.DmImageMapper;
import cn.kgc.pojo.DmImage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by 北大课工场
*/
@RestController
public class RestDmImageService {

@Autowired
private DmImageMapper dmImageMapper;

    //获取默认轮播图,,可能会有多个轮播图，也可能一个也没有
    @PostMapping(value = "/getDmImageDefaultImage")
    public List<DmImage> getDmImage_DefaultImage(
                                                @RequestParam Long id,
                                                @RequestParam Integer type,
                                                @RequestParam Integer category) throws Exception {
        Map imagesMap= new HashMap<>();
        imagesMap.put("targetId",id);
        imagesMap.put("type", type);
        imagesMap.put("category",category);

        List<DmImage> dmImageListByMap = dmImageMapper.getDmImageListByMap(imagesMap);
        if(dmImageListByMap==null||dmImageListByMap.size()==0){
            DmImage dmImage=new DmImage();
                //如果轮播图的类别跟商品表中的类别一致，则把它的url设置成默认录播图
            if (category==Constants.Image.ImageCategory.item) {
                dmImage.setImgUrl(Constants.DEFAULT_CAROUSEL);
            } else if (category==Constants.Image.ImageCategory.user){
                dmImage.setImgUrl(Constants.DEFAULT_USER);
            }
            dmImageListByMap.add(dmImage);
        }
        return dmImageListByMap;
    }

@RequestMapping(value = "/getDmImageById",method = RequestMethod.POST)
public DmImage getDmImageById(@RequestParam("id") Long id)throws Exception{
return dmImageMapper.getDmImageById(id);
}

@RequestMapping(value = "/getDmImageListByMap",method = RequestMethod.POST)
public List<DmImage>  getDmImageListByMap(@RequestParam Map
<String,Object> param)throws Exception{
return dmImageMapper.getDmImageListByMap(param);
}

@RequestMapping(value = "/getDmImageCountByMap",method = RequestMethod.POST)
public Integer getDmImageCountByMap(@RequestParam Map
<String,Object> param)throws Exception{
return dmImageMapper.getDmImageCountByMap(param);
}

@RequestMapping(value = "/qdtxAddDmImage",method = RequestMethod.POST)
public Integer qdtxAddDmImage(@RequestBody DmImage dmImage)throws Exception{
dmImage.setCreatedTime(new Date());
return dmImageMapper.insertDmImage(dmImage);
}

@RequestMapping(value = "/qdtxModifyDmImage",method = RequestMethod.POST)
public Integer qdtxModifyDmImage(@RequestBody DmImage dmImage)throws Exception{
dmImage.setUpdatedTime(new Date());
return dmImageMapper.updateDmImage(dmImage);
}

@RequestMapping(value = "/qddelDmImage",method = RequestMethod.GET)
public Integer qdtxDelDmImage(@RequestParam("id") Long id)throws Exception{

return dmImageMapper.deleteDmImageById(id);
}


}
